//
//  UIBarButtonItem+Extension.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/2/3.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    //自定义UIBarButtonItem的便利构造函数
    convenience init (imageName: String, target: AnyObject?, actionName: String?){
        let button = UIButton(type: .Custom)
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        if let action = actionName, targetObj = target {
            button.addTarget(targetObj, action: Selector(action), forControlEvents: .TouchUpInside)
        }
        button.sizeToFit()
        self.init(customView: button)
    }
    
    // 如果在func前面加上class, 就相当于OC中的+
    class func creatBarButtonItem(imageName:String, target: AnyObject?, action:Selector) ->UIBarButtonItem {
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return UIBarButtonItem(customView: btn)
    }

}
